<template>
	<view>
		<uni-notice-bar show-icon text="密码生成后自动复制!(本地缓存,无数据库,gitee开源<原密码>)" />
		<view class="container" style="padding:2px ;">
			<uni-group>
			<view class="uni-form-item uni-column">
				<view class="uni-form-item uni-column">
					<view class="title">原密码</view>
					<uni-easyinput  type="password" v-model="orgin_password" :styles="styles" :placeholderStyle="placeholderStyle" ></uni-easyinput>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title">软件名:如微信,QQ</view>
					<uni-easyinput  v-model="name"  :styles="styles" :placeholderStyle="placeholderStyle" ></uni-easyinput>
				</view>
				<button class="uni-button" type="primary" @click="setText" style="margin-top: 1rem;">生成密码</button>
				<view class="title">新密码：{{new_password}}</view>
			</view>
			</uni-group>
			
			<!-- <uni-group> -->
			<view class="uni-container">
				<view class="title">快速生成</view>
				<view v-for="(item, index) in app_name" :key="index">
					<button class="uni-button" size="mini" type="primary" style="float: left;margin: 2rpx;" @click="getPassword" :data-app_name="item">{{item?item:'空'}}</button>
				</view>
				<!-- <view style="float:none;"></view> -->
			</view>
			<!-- </uni-group> -->
		
		</view>
	</view>
</template>

<script>
	import password from "../../util/password.js";
	export default {
		data() {
			return {
				app_name:['steam','steam2','QQ','QQ2','门锁','门锁2','A密码','B密码','C密码','D密码','E密码','F密码','G密码','H密码','github','gitee','慕课','JD','微信','其他'],
				placeholderStyle: "color:#2979FF;font-size:14px",
				styles: {
					color: '#2979FF',
					borderColor: '#2979FF'
				},
				new_password:'',
				orgin_password:'',
				password_number:8,
				password_type:{0:["0","1","2","3","4","5","6","7","8","9"],
				1:["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
				},
				name:'',
				// auth:false
			}
		},
		onLaunch: function() {
			console.log('App Launch')
		},
		onShow: function() {
			var that=this;
			console.log(that.$data)
			try {
				// 如果首次进入
				const index_data = uni.getStorageSync('index_data');
				var auth=uni.getStorageSync('auth');
				if(index_data&&auth&&auth=='no'){
					if (index_data) {
						Object.assign(that.$data,index_data);
					}
					that.orgin_password='';
					that.new_password='';
					
					var orgin_password=index_data.orgin_password;
					if(orgin_password){
						// 指纹验证后保障下一次使用
						try {
						uni.startSoterAuthentication({
							requestAuthModes: ['fingerPrint'],
							challenge: '123456',
							authContent: '验证指纹后，可获得缓存中的“原密码”，也可选择“取消”',
							success(res) {
								if (orgin_password) {
									that.orgin_password=orgin_password;
								}
							},
							fail(err) {
								console.log(err);
							},
							complete(res) {
								uni.setStorageSync('auth','yes');
								// that.auth=true;
								console.log(res);
							}
						})
						} catch (e) {
							// error
						}
					}
					
				}
				
				// that.auth=false;
				uni.setStorageSync('auth','no');
			} catch (e) {
				// error
			}
			console.log('App Show')
		},
		onHide: function() {
			this.orgin_password='';
			console.log('App Hide')
		},
		// watch: {
		//   'orgin_password': function(newVal,oldVal){
		// 	  uni.setStorageSync('orgin_password',newVal);
		//   }
		// },
		methods: {
			setText() {
				this.new_password=this.getNewPassword(this.orgin_password,this.name,this.password_number,this.password_type);
			},
			getPassword(event){
				var el = event.currentTarget;
				this.name=el.dataset.app_name;console.log(1);console.log(this.password_type);console.log(2);
				this.new_password=this.getNewPassword(this.orgin_password,this.name,this.password_number,this.password_type);
			},
			getNewPassword(origin_password,encrypt_factor='',password_number=0,format=[]){
				var that=this;
				var new_password=password.createNewPassword(origin_password,encrypt_factor,password_number,format);
				uni.setClipboardData({
				    data: new_password,
					showToast:false,
				    success: function () {
				        try {
				        	// 判断是否缓存中存在该app名
							for(var i in that.app_name){
								if(that.app_name[i]==encrypt_factor){
									that.app_name.splice(i, 1);
									break;
								}
							}

							that.app_name.unshift(encrypt_factor);
							
							uni.setStorageSync('index_data',that.$data);
							
							// 最多30个app
				            if(that.app_name.length>30){
				            	that.app_name.pop();
				            }
				        } catch (e) {
				            // error
				        }
				    }
				});
				return new_password;
			},
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>
